//
//  GYFloatingView.m
//  DriverApp
//
//  Created by zgy on 2025/4/21.
//  Copyright © 2025 LS-LONG. All rights reserved.
//

#import "GYFloatingView.h"

@implementation GYFloatingView
{
    CGPoint lastLocation;
    UILabel *_lab;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    // 创建毛玻璃效果
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterial];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurEffectView.frame = self.bounds;
    blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    blurEffectView.alpha = 0.7; // 设置毛玻璃层的透明度
    
    // 添加毛玻璃视图为背景
    [self addSubview:blurEffectView];
    
//    // 添加半透明覆盖层（可选）
//    UIView *translucentOverlay = [[UIView alloc] initWithFrame:self.bounds];
//    translucentOverlay.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.2]; // 半透明白色叠加层
//    translucentOverlay.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    [self addSubview:translucentOverlay];
    
    // 圆角设置
    self.layer.cornerRadius = 5; // 圆角半径
    self.layer.masksToBounds = YES;
    
    
    _lab = [[UILabel alloc] initWithFrame:self.bounds];
    _lab.text = @" V5.49.1(1) ";
    _lab.adjustsFontSizeToFitWidth = YES;
    [self addSubview:_lab];
    
    
    // 添加拖动手势
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self addGestureRecognizer:panGesture];
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture {
    CGPoint translation = [gesture translationInView:self.superview];
    
    // 更新视图位置
    self.center = CGPointMake(self.center.x + translation.x, self.center.y + translation.y);
    
    // 重置手势的偏移量
    [gesture setTranslation:CGPointZero inView:self.superview];
    
    // 手势结束后回弹到屏幕边缘
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self adjustPositionToScreenBounds];
    }
}


// 将视图调整到屏幕边缘
- (void)adjustPositionToScreenBounds {
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;

    CGFloat viewWidth = self.bounds.size.width;
    CGFloat viewHeight = self.bounds.size.height;

    CGFloat newX = self.center.x;
    CGFloat newY = self.center.y;

    // 限制 X 轴范围
    if (self.frame.origin.x < 0) {
        newX = viewWidth / 2; // 左边界
    } else if (self.frame.origin.x + viewWidth > screenWidth) {
        newX = screenWidth - viewWidth / 2; // 右边界
    }

    // 限制 Y 轴范围
    if (self.frame.origin.y < 0) {
        newY = viewHeight / 2; // 上边界
    } else if (self.frame.origin.y + viewHeight > screenHeight) {
        newY = screenHeight - viewHeight / 2; // 下边界
    }

    // 动画移动到新位置
    [UIView animateWithDuration:0.3 animations:^{
        self.center = CGPointMake(newX, newY);
    }];
}




@end
